using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using NHibernate.Data.Managers;
using NHibernate.Data.Models;
using NHibernate.Data.Base;

namespace NHibernate.Data.UnitTests
{
	[TestFixture]
    public partial class PersonTests : UNuitTestBase
    {
        protected IPersonManager manager;
		
		public PersonTests()
        {
            manager = managerFactory.GetPersonManager();
        }
		
		protected Person CreateNewPerson()
		{
			Person entity = new Person();
			
			// You may need to maually enter this key if there is a constraint violation.
			entity.Id = default(Int64);
			
			entity.Corporation = default(Int64);
			entity.Winery = default(Int64);
			entity.LocalName = "Test Test ";
			entity.InternationalName = "Test Test ";
			entity.Telephone = "Test Test ";
			entity.Fax = "Test Test ";
			entity.Msn = "Test Test ";
			entity.Skype = "Test Test ";
			entity.Qq = "Test Test ";
			entity.Aol = "Test Test ";
			entity.Icq = "Test Test ";
			entity.address = "Test Test ";
			entity.Zip = "Test Test ";
			entity.Email = "Test Test ";
			entity.Introduction = "Test Test ";
			entity.Category = default(Int64);
			entity.IsCorporation = true;
			entity.IsWinery = true;
			
			return entity;
		}
		protected Person GetFirstPerson()
        {
            IList<Person> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				Person entity = CreateNewPerson();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Read()
        {
            try
            {
                Person entityA = CreateNewPerson();
				manager.Save(entityA);

                Person entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                Person entityA = GetFirstPerson();
				entityA.Corporation = default(Int64);
				
				manager.Update(entityA);

                Person entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.Corporation, entityB.Corporation);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                Person entity = GetFirstPerson();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
	}
}